import { useStore } from "@/store"
import { ref, watch, onMounted, computed } from "vue-demi"
function useState() {
  const store = useStore()
  const editorRef = ref()
  onMounted(() => {
    watch(
      () => editorRef.value.titles,
      async () => {
        store.commit("editor/setEditorRef", editorRef.value)
      }
    )
    watch(
      () => store.state.markdown.id,
      async () => {
        if (store.state.editor.editMode === "preview") {
          editorRef.value.previewScrollTo(0)
        }
      }
    )
  })

  const content = computed({
    get() {
      return store.state.markdown.content
    },
    set(val: string) {
      store.commit("markdown/changeContent", val)
    },
  })
  return { editorRef, content }
}
export default useState
